package com.nyl.learn.client;


import com.nyl.learn.model.HolidayResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.xml.transform.StringSource;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.util.JAXBResult;
import javax.xml.transform.stream.StreamSource;

/**
 * Created by nyl
 * 2017/6/23
 */
public class SpringClient {
    
    private final WebServiceTemplate client = new WebServiceTemplate();
    
    
    @Test
    public void sptingTest() {
        
        client.setDefaultUri("http://www.ningyongli.site:8080/webservicelearn/holidayService");
    
        StreamSource source =  new StringSource(
        
                "<holidayRequest xmlns='http://mycompany.com/hr/webservice'>" +
                
                        "<name>旺财</name>" +
                
                        "</holidayRequest>");
    
        try {
            JAXBContext unmarshaller = JAXBContext.newInstance(HolidayResponse.class);
            JAXBResult jaxbResult = new JAXBResult(unmarshaller);
            
            //StreamResult result = new StreamResult(System.out);
            client.sendSourceAndReceiveToResult(source, jaxbResult);
            HolidayResponse holidayResponse = (HolidayResponse) jaxbResult.getResult();
            System.out.println(holidayResponse.toString());
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    
        
        
    }
    
    
    @Test
    public void httpclientTest() {
        CloseableHttpClient closeableHttpClient = null;
        CloseableHttpResponse closeableHttpResponse = null;
        String  result = null;
        try {
            
            String requestStr = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
                    "xmlns:web=\"http://mycompany.com/hr/webservice\">" +
                    "<soapenv:Header/>" +
                    "<soapenv:Body>"+
                    "<web:holidayRequest>" +
                    "<web:name>旺财</web:name>" +
                    "</web:holidayRequest>" +
                    "</soapenv:Body>" +
                    "</soapenv:Envelope>";
                
            HttpPost httpPost = new HttpPost("http://www.ningyongli.site:8080/webservicelearn/holidayService");
            httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");
            //httpPost.setHeader("SOAPAction", "hollidayService");
    
            System.out.println(requestStr);
            StringEntity entity = new StringEntity(requestStr, "UTF-8");
            httpPost.setEntity(entity);
        
            closeableHttpClient = HttpClients.createDefault();
            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            result = EntityUtils.toString(closeableHttpResponse.getEntity());
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                closeableHttpResponse.close();
                closeableHttpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
   
    
    
}
